package com.bluegalaxy.nhb.snservice.service.inquery.impl;

import com.alibaba.fastjson.JSONArray;
import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.dao.InqueryTemplateMapper;
import com.bluegalaxy.nhb.snservice.domain.model.InqueryTemplate;
import com.bluegalaxy.nhb.snservice.domain.vo.BootstrapTableParam;
import com.bluegalaxy.nhb.snservice.domain.vo.inquery.InqueryWhere;
import com.bluegalaxy.nhb.snservice.service.inquery.InqueryService;
import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.dao.InqueryTemplateMapper;
import com.bluegalaxy.nhb.snservice.domain.model.InqueryTemplate;
import com.bluegalaxy.nhb.snservice.domain.vo.BootstrapTableParam;
import com.bluegalaxy.nhb.snservice.domain.vo.inquery.InqueryWhere;
import com.bluegalaxy.nhb.snservice.service.inquery.InqueryService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Date 2016/5/17 15:49
 * @Description 
 **/
@Service("InqueryService")
public class InqueryServiceImpl implements InqueryService {


//
//    @Autowired
//    private InqueryTemplateMapper inqueryTemplateMapper;
//
//    private Logger log = LoggerFactory.getLogger(InqueryServiceImpl.class);
//
//    @Override
//    public InqueryTemplate getInqueryTemplateById(int id) {
//        return this.inqueryTemplateMapper.selectByPrimaryKey(id);
//    }
//
//    @Override
//    public List<Map> queryInquery(InqueryTemplate inquery, BootstrapTableParam bootstrapTableParam, Map requestParamMap, Map sessionMap) {
//        String sql = createSql(inquery, bootstrapTableParam,requestParamMap,sessionMap);
//        return this.inqueryTemplateMapper.executeSqlQuery(sql);
//    }
//
//    /**
//     * 处理排序
//     *
//     * @param bootstrapTableParam
//     * @return
//     */
//    @Override
//    public String executeSort(BootstrapTableParam bootstrapTableParam) {
//        StringBuffer sb = new StringBuffer(" ");
//        if (StringUtil.isNotEmpty(bootstrapTableParam.getSort())) {
//            sb.append(" order by ")
//                    .append(bootstrapTableParam.getSort())
//                    .append(" ")
//                    .append(bootstrapTableParam.getOrder());
//        }
//        return sb.toString();
//    }
//
//    @Override
//    public String createSql(InqueryTemplate inquery, BootstrapTableParam bootstrapTableParam,
//                            Map<String,String[]> requestMap,Map<String,String> sessionMap) {
//        //解析inquery的where,
//        //StringBuffer sb = new StringBuffer(inquery.getSqlConfig()).append(" ");
//
//        List<InqueryWhere> wheres = JSONArray.parseArray(inquery.getWhereConfig(), InqueryWhere.class);
//
//        //封装where
//        Map<String, InqueryWhere> whereMap = new HashMap<>();
//        for (InqueryWhere where : wheres) {
//            whereMap.put(where.getKey(), where);
//        }
//
//        //拿到sql配置
//        String sqlconfig = inquery.getSqlConfig().replace("\n", "");
//        StringBuilder sql = new StringBuilder();
//        //解析where
//        String[] sqlplus = sqlconfig.split("\\[");
//        for (String s : sqlplus) {
//            if (s.contains("]")) {
//                String[] tmpSql = s.split("]");
//                String where = tmpSql[0];
//                String[] whereKeys = where.split("#");
//                if (whereKeys.length > 1) {
//                    String queryParams = whereKeys[1];
//                    InqueryWhere where1 = whereMap.get(queryParams);
//                    if (where1 != null) {
//                        //session
//                        if (InqueryWhere.TYPE_SESSION.equals(where1.getType())) {
//                            Object value =sessionMap.get(where1.getKey());
//                            if (value != null) {
//                                where = where.replace("#" + queryParams + "#", String.valueOf(value));
//                                sql.append(where).append(" ");
//                            }
//                        } else if (InqueryWhere.TYPE_SEARCHER.equals(where1.getType())) {
//                            //searcher
//                            if (StringUtil.isNotEmpty(bootstrapTableParam.getSearch())) {
//                                where = where.replace("#" + queryParams + "#", bootstrapTableParam.getSearch());
//                                sql.append(where).append(" ");
//                            }
//
//                        } else if (InqueryWhere.TYPE_PAGE.equals(where1.getType())) {
//                            //page
//                            //多参数
//                            if ("checkbox".equals(where1.getTag().getTag_type())) {
//                                if (requestMap.get(queryParams) != null) {
//                                    StringBuilder value = new StringBuilder();
//                                    String[] obj = requestMap.get(queryParams);
//                                    for (String v : obj) {
//                                        value.append("'").append(v).append("'").append(",");
//                                    }
//                                    if (value.length() > 0) {
//                                        where = where.replace("#" + queryParams + "#", value.substring(0, value.length() - 1));
//                                        sql.append(where).append(" ");
//                                    }
//                                }
//                            } else if ("select".equals(where1.getTag().getTag_type())) {
//                                if (requestMap.get(queryParams) != null && requestMap.get(queryParams).length > 0 && !"-1".equals(requestMap.get(queryParams)[0])) {
//                                    String value = requestMap.get(queryParams)[0];
//                                    where = where.replace("#" + queryParams + "#", value);
//                                    sql.append(where).append(" ");
//                                }
//                            } else if ("textarea".equals(where1.getTag().getTag_type())) {
//                                if (requestMap.get(queryParams) != null && requestMap.get(queryParams).length > 0 && StringUtil.isNotEmpty(requestMap.get(queryParams)[0])) {
//                                    String value = requestMap.get(queryParams)[0];
//                                    String newValue = "";
//                                    String[] values = value.split("\n");
//                                    for (String value1 : values) {
//                                        newValue += "\'" + value1.trim() + "\',";
//                                    }
//                                    where = where.replace("#" + queryParams + "#", newValue.substring(0, newValue.length() - 1));
//                                    sql.append(where).append(" ");
//                                }
//                            } else {
//                                if (requestMap.get(queryParams) != null && requestMap.get(queryParams).length > 0 && StringUtil.isNotEmpty(requestMap.get(queryParams)[0])) {
//                                    String value = requestMap.get(queryParams)[0];
//                                    where = where.replace("#" + queryParams + "#", value);
//                                    sql.append(where).append(" ");
//                                }
//                            }
//                        }
//                    }
//                }
//                if (tmpSql.length > 1) {
//                    sql.append(tmpSql[1]).append(" ");
//                }
//            } else {
//                sql.append(s).append(" ");
//            }
//        }
//        log.info("  -------------------sql:  " + sql.toString());
//        //order by
//        sql.append(executeSort(bootstrapTableParam));
//        return sql.toString();
//    }

}
